/**
 * Created by vincent on 2017/9/18.
 */
moduleStudyMap.controller("EditStudyClassController", function ($$CambioConfig, $scope, $log, $state, LoadingService, UploadFileService,
                                                                ConstantService, DialogService, EnumService, StudyClassResource) {
    $scope.$on("ngDialog.opened", function () {
        dialogOnLoad();
    });

    $scope.uploadImage = function ($files) {
        if ($files && $files[0]) {
            UploadFileService.uploadFile($files[0], $$CambioConfig.UPLOAD_URL, 'IMG', $scope).then(function (o) {
                $scope.editingStudyClass.picture = o.id;
            });
        }
    };

    $scope.cancel = function () {
        DialogService.close();
    };

    $scope.submit = function () {
        var currentStudyClassGroupId = ConstantService.getItem("currentStudyClassGroupId");
        if ($scope.editSCForm.$valid && !StringUtils.isNlOrUndOrEmpty(currentStudyClassGroupId)) {
            //LoadingService.show();
            if (!StringUtils.isNlOrUndOrEmpty($scope.editingStudyClass.typeOption)) {
                $scope.editingStudyClass.type = EnumService.getEnumKey($scope.editingStudyClass.typeOption);
            }
            $scope.editingStudyClass.studyClassGroup = {id: currentStudyClassGroupId};
            StudyClassResource.update($scope.editingStudyClass, function (classResult) {
                $log.debug("课程更新结果", classResult);
                angular.copy(classResult, $scope.currentStudyClass);
                DialogService.close();
                //LoadingService.hide();
                Alert.success("更新课程成功！");
            }, function () {
                //LoadingService.hide();
            });
        }
    };

});